package com.hunktimes.wholesale.widget

import android.content.Context
import android.os.CountDownTimer
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.hunktimes.wholesale.R

/**
 * Author: Delete
 * Date: 2020/8/7 17:00
 * Description:
 * 倒计时
 */
class CountDown(
    val context: Context, val target: TextView,
    seconds: Int = 60
) : CountDownTimer(seconds * 1000L, 1000L),LifecycleObserver {
    override fun onFinish() {
        target.isEnabled = true
        target.setText(context.getString(R.string.send_again))
    }

    override fun onTick(millisUntilFinished: Long) {
        if (target.isEnabled) {
            target.isEnabled = false
        }
        val leftTime = (millisUntilFinished / 1000L).toInt()
        target.setText(context.getString(R.string.sms_countdown_pattern, leftTime))
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun cancelWrap() {
        onFinish()
        cancel()
    }
}